//
//  NovelViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-13.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NovelViewController.h"
#import "NovelCategoryCell.h"
#import "AppDelegate.h"
#import "MagazineCategoryWidget.h"
#import "DataRequest.h"
#import "NovelViewCategory.h"
#import "NovelShelfViewController.h"
//#import "NovelCategoryWidget.h"
#import "SearchViewController.h"
@interface NovelViewController ()
{
    MagazineCategoryWidget *magazineCategoryWidget;
    NovelViewCategory * novelView;
}
@property (nonatomic,retain) NSArray *categoryArray;
@end

@implementation NovelViewController
- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:categoryTableView.frame] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NovelCategoryCellDidSelect:) name:@"NovelCategoryCellDidSelect" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = self.parentViewController.view.bounds;
    [self getData];
    novelView = [[NovelViewCategory alloc]initWithFrame:categoryTableView.frame];
}

-(void)NovelCategoryCellDidSelect:(NSNotification *)notifi{
    [headImage setImage:[UIImage imageNamed:@"top_bar_bg.png"]];
    titleLabel.text = [notifi.object objectForKey:@"class_name"];
    if (notifi.object) {
        if (novelView.superview) {
            [novelView removeFromSuperview];
        }
        NSDictionary * dict = notifi.object;
        [self showLoadingView];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            id obj = [[DataRequest sharedRequest] getNovelByCategory:[dict objectForKey:@"class_id"] count:100];
            NSArray * array = [[obj objectForKey:@"result"] objectForKey:@"novel_list"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self removeloadingView];
                [self.view addSubview:novelView];
                novelView.category = array;
            });
        });
    }else{
        [headImage setImage:[UIImage imageNamed:@"new_logo_top_bar.png"]];
        titleLabel.text = nil;
        [novelView removeFromSuperview];
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (magazineCategoryWidget == nil) {
        magazineCategoryWidget = [[MagazineCategoryWidget alloc] initWithFrame:CGRectMake(0, 0, 320, 548)];
        [magazineCategoryWidget updateCategory:_categoryArray key:@"class_name"];
    }
    if (magazineCategoryWidget.superview == nil) {
        [[UIApplication rootViewController] updateWidgetView:magazineCategoryWidget];
    }
    NSLog(@">>>>>>>");
}
- (void)getData
{
    [self showLoadingView];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getNovelCategory];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self removeloadingView];
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                self.categoryArray = [data objectForKey:@"result"];
                [categoryTableView reloadData];
                [magazineCategoryWidget updateCategory:_categoryArray key:@"class_name"];
            }
        });
    });
}
- (IBAction)searchBtnClicked:(id)sender {
    SearchViewController * searchViewController = [[[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil]autorelease];
    searchViewController.type = searchNovel;
    [[UIApplication rootNavController] pushViewController:searchViewController animated:YES];

}
- (IBAction)showShelf:(id)sender {
    NovelShelfViewController * novelShelfViewController = [[[NovelShelfViewController alloc]initWithNibName:@"NovelShelfViewController" bundle:nil]autorelease];
    [[UIApplication rootNavController] pushViewController:novelShelfViewController animated:YES];
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _categoryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentify = @"NovelCategoryCell";
    NovelCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[[NovelCategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify] autorelease];
    }
    [cell setCategory:[_categoryArray objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 155;
}

- (IBAction)showCategory
{
    [[UIApplication rootViewController] showWidgetView];
}

- (void)swipeBack
{
    [self showCategory];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_categoryArray release];
    [categoryTableView release];
    [titleLabel release];
    [headImage release];
    [super dealloc];
}

- (void)viewDidUnload {
    [categoryTableView release];
    categoryTableView = nil;
    [titleLabel release];
    titleLabel = nil;
    [headImage release];
    headImage = nil;
    [super viewDidUnload];
}
@end
